home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / linux-bo / comboot-.0 / comboot- / comboot-1.0 / makebootsect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-25  |  3.1 KB  |  136 lines

  1. /*
  2.  *  linux/tools/build.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. /*
  8.  * This file builds a disk-image from three different files:
  9.  *
  10.  * - bootsect: exactly 512 bytes of 8086 machine code, loads the rest
  11.  * - setup: 8086 machine code, sets up system parm
  12.  * - system: 80386 code for actual system
  13.  *
  14.  * It does some checking that all files are of the correct type, and
  15.  * just writes the result to stdout, removing headers and padding to
  16.  * the right amount. It also writes some system data to stderr.
  17.  */
  18.  
  19. /*
  20.  * Changes by tytso to allow root device specification
  21.  * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
  22.  */
  23.  
  24. #include <stdio.h>    /* fprintf */
  25. #include <string.h>
  26. #include <stdlib.h>    /* contains exit */
  27. #include <sys/types.h>    /* unistd.h needs this */
  28. #include <sys/stat.h>
  29. #include <sys/sysmacros.h>
  30. #include <unistd.h>    /* contains read/write */
  31. #include <fcntl.h>
  32. #include <linux/a.out.h>
  33. #include <linux/config.h>
  34. #include <errno.h>
  35.  
  36. #define MINIX_HEADER 32
  37.  
  38. #define N_MAGIC_OFFSET 1024
  39. #ifndef __BFD__
  40. static int GCC_HEADER = sizeof(struct exec);
  41. #endif
  42.  
  43. #ifdef __BIG_KERNEL__
  44. #define SYS_SIZE 0xffff
  45. #else
  46. #define SYS_SIZE DEF_SYSSIZE
  47. #endif
  48.  
  49. #define DEFAULT_MAJOR_ROOT 0
  50. #define DEFAULT_MINOR_ROOT 0
  51.  
  52. /* max nr of sectors of setup: don't change unless you also change
  53.  * bootsect etc */
  54. #define SETUP_SECTS 4
  55.  
  56. #define STRINGIFY(x) #x
  57.  
  58. typedef union {
  59.     long l;
  60.     short s[2];
  61.     char b[4];
  62. } conv;
  63.  
  64. long intel_long(long l)
  65. {
  66.     conv t;
  67.  
  68.     t.b[0] = l & 0xff; l >>= 8;
  69.     t.b[1] = l & 0xff; l >>= 8;
  70.     t.b[2] = l & 0xff; l >>= 8;
  71.     t.b[3] = l & 0xff; l >>= 8;
  72.     return t.l;
  73. }
  74.  
  75. short intel_short(short l)
  76. {
  77.     conv t;
  78.  
  79.     t.b[0] = l & 0xff; l >>= 8;
  80.     t.b[1] = l & 0xff; l >>= 8;
  81.     return t.s[0];
  82. }
  83.  
  84. void die(const char * str)
  85. {
  86.     fprintf(stderr,"%s\n",str);
  87.     exit(1);
  88. }
  89.  
  90. void usage(void)
  91. {
  92.     die("Usage: makebootsect as86-image > bootsect");
  93. }
  94.  
  95. int main(int argc, char ** argv)
  96. {
  97.     int i,c,id, sz;
  98.     unsigned long sys_size;
  99.     char buf[1024];
  100. #ifndef __BFD__
  101.     struct exec *ex = (struct exec *)buf;
  102. #endif
  103.     char major_root, minor_root;
  104.     struct stat sb;
  105.     unsigned char setup_sectors;
  106.  
  107.     if (argc != 2) usage();
  108.     for (i=0;i<sizeof buf; i++) buf[i]=0;
  109.     if ((id=open(argv[1],O_RDONLY,0))<0)
  110.         die("Unable to open 'boot'");
  111.     if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
  112.         die("Unable to read header of 'boot'");
  113.     if (((long *) buf)[0]!=intel_long(0x04100301))
  114.         die("Non-Minix header of 'boot'");
  115.     if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
  116.         die("Non-Minix header of 'boot'");
  117.     if (((long *) buf)[3] != 0)
  118.         die("Illegal data segment in 'boot'");
  119.     if (((long *) buf)[4] != 0)
  120.         die("Illegal bss in 'boot'");
  121.     if (((long *) buf)[5] != 0)
  122.         die("Non-Minix header of 'boot'");
  123.     if (((long *) buf)[7] != 0)
  124.         die("Illegal symbol table in 'boot'");
  125.     i=read(id,buf,sizeof buf);
  126.     fprintf(stderr,"Boot sector %d bytes.\n",i);
  127.     if (i != 512)
  128.         die("Boot block must be exactly 512 bytes");
  129.     if ((*(unsigned short *)(buf+510)) != (unsigned short)intel_short(0xAA55))
  130.         die("Boot block hasn't got boot flag (0xAA55)");
  131.     buf[508] = (char) minor_root;
  132.     buf[509] = (char) major_root;    
  133.     i=write(1,buf,512);
  134.     return(0);
  135. }
  136.